// pages/my/teacher/homeworkDetail/homeworkDetail.js
const app = getApp()
var base_url = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: "",//用户头像
      nickName: "",//用户昵称
    },
    b1: 'dimgray',
    b2: 'dimgray',
    b3: 'dimgray',
    bgdata: 0,
    nowCount: 1,
    bgcolor: '#169bd5',
    commentstr: '点评下一位同学',
    none1: 'block',
    none2: 'none',
    display1: 'block',
    display2: 'none',
    display3: 'block',
    touch: {
      distance: 0,
      scale: 1,
      baseWidth: null,
      baseHeight: null,
      scaleWidth: null,
      scaleHeight: null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    /**
     * 获取用户信息
     */
    // wx.getUserInfo({
    //   success: function (res) {
    //     console.log(res);
    //     var avatarUrl = 'userInfo.avatarUrl';
    //     var nickName = 'userInfo.nickName';
    //     that.setData({
    //       [avatarUrl]: res.userInfo.avatarUrl,
    //       [nickName]: res.userInfo.nickName,
    //     })
    //   }
    // })


    wx.getStorage({
      key: 'uhid',
      success: function (res) {
        wx.request({
          url: base_url + '/getExerciseDetail/',
          method: 'POST',
          data: {
            uhid: res.data
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if (res.data['flag'] == true) {
              that.setData({
                headpic: res.data['data_dic']['headpic'],
                sysFile: res.data['data_dic']['sysFile']
              })
              that.setData({
                markPic: res.data['data_dic']['markPic']
              })
              console.log(res)
              if (res.data['data_dic']['finalPicpath'] == '') {
                that.setData({
                  imgPath: res.data['data_dic']['path']
                })
              } else {
                that.setData({
                  imgPath: res.data['data_dic']['finalPicpath']
                })
              }

              that.setData({
                name: res.data['data_dic']['name']
              })
              console.log(res.data['data_dic']['comment'])
              if (res.data['data_dic']['comment'] == '优') {
                that.btn_1()
              } else if (res.data['data_dic']['comment'] == '良') {
                that.btn_2()
              } else if (res.data['data_dic']['comment'] == '中') {
                that.btn_3()
              }
              that.setData({
                textarea: res.data['data_dic']['autoComment']
              })
              wx.setStorage({
                key: 'tempImageSrc',
                data: that.data.imgPath,
                success: function (res) {
                }
              })


            }
          }
        })
      },
    })
    wx.getSystemInfo({

      success: function (res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;

        let itemheight = clientWidth * 0.94 * 0.15
        that.setData({
          itemheight: itemheight
        })
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  btn_1: function () {
    this.setData({
      b1: 'linear-gradient(to right, #01B6B1, #03CD9E)',
      b2: 'dimgray',
      b3: 'dimgray',
      bgdata: 1
    })
  },
  btn_2: function () {
    this.setData({
      b1: 'dimgray',
      b2: 'linear-gradient(to right, #01B6B1, #03CD9E)',
      b3: 'dimgray',
      bgdata: 2
    })
  },
  btn_3: function () {
    this.setData({
      b1: 'dimgray',
      b2: 'dimgray',
      b3: 'linear-gradient(to right, #01B6B1, #03CD9E)',
      bgdata: 3
    })
  },
  gotoPtword: function () {
    var that = this;
    that.setData({
      none1: 'block',
      none2: 'none',
      display1: 'block',
      display2: 'none',
      display3: 'block',
    })
  },
  gotoEditImage: function () {
    var that = this;
    // that.setData({
    //   none1: 'none',
    //   none2: 'block',
    //   display1: 'none',
    //   display2: 'block',
    //   display3: 'none',
    // })

    wx.navigateTo({
      url: '../editImage/editImage',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  gotoComment: function () {
    var that = this;
    console.log(this.data.bgdata)
    if (this.data.nowCount == this.data.ccount) {
      this.setData({
        bgcolor: 'gray',
        commentstr: '点评结束'
      })
    }
  },
  tosinResult: function () {
    wx.navigateTo({
      url: '../../../photo/sinResult/sinResult',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  setGoodExercise: function () {
    wx.getStorage({
      key: 'uhid',
      success: function (res) {
        wx.request({
          url: base_url + '/goodExerciseSend/',
          method: 'POST',
          data: {
            uhid: res.data
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            if (res.data['flag'] == true) {
              wx.showModal({
                title: '推送成功',
                content: '',
                success: function (res) {
                  if (res.confirm) {
                    console.log('弹框后点确定')
                  } else {
                    console.log('弹框后点取消')
                  }
                }
              })
            }
          }
        })
      },
    })
  },
  touchstartCallback: function (e) {
    // 单手指缩放开始，也不做任何处理 
    if (e.touches.length == 1) return console.log('双手指触发开始')
    // 注意touchstartCallback 真正代码的开始 // 一开始我并没有这个回调函数，会出现缩小的时候有瞬间被放大过程的bug // 当两根手指放上去的时候，就将distance 初始化。 
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    this.setData({
      'touch.distance': distance,
    })
  },
  touchmoveCallback: function (e) {
    let touch = this.data.touch
    // 单手指缩放我们不做任何操作 
    if (e.touches.length == 1) return console.log('双手指运动')
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    // 新的 ditance 
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    let distanceDiff = distance - touch.distance;
    let newScale = touch.scale + 0.005 * distanceDiff
    // 为了防止缩放得太大，所以scale需要限制，同理最小值也是 
    if (newScale >= 2) {
      newScale = 2
    }
    if (newScale <= 0.6) {
      newScale = 0.6
    }
    let scaleWidth = newScale * touch.baseWidth
    let scaleHeight = newScale * touch.baseHeight
    // 赋值 新的 => 旧的 
    this.setData({
      'touch.distance': distance,
      'touch.scale': newScale,
      'touch.scaleWidth': scaleWidth,
      'touch.scaleHeight': scaleHeight,
      'touch.diff': distanceDiff
    })
  },
  bindload: function (e) {
    // bindload 这个api是<image>组件的api类似<img>的onload属性 
    this.setData({
      'touch.baseWidth': e.detail.width,
      'touch.baseHeight': e.detail.height,
      'touch.scaleWidth': e.detail.width,
      'touch.scaleHeight': e.detail.height
    })
  },
  toEditImg: function () {
    wx.navigateTo({
      url: '../editImage/editImage',
      success: function (res) {
        console.log(res)
      },
      fail: function () {
      }
    })
  },
  toseePic:function(){
    var that = this
    wx.previewImage({
      current: that.data.imgPath, //当前图片地址  //所有要预览的图片的地址集合数组形式
      urls: [that.data.imgPath], //一张图写法
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})